<?php

// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------
namespace app\order\admin;

use app\admin\admin\Base;
use think\Db;
use service\Format;
use app\common\model\Order as OrderModel;
use app\order\model\OrderGoods;

/**
 * 订单打印
 * @author 晓风<215628355@qq.com>
 * @date 2021-11-6 17:24:49
 */
class Prints extends Base {

   
    
    /**
     * 订单打印
     * @param $order_sn
     * @author jxy [ 415782189@qq.com ]
     * @created 2020/5/11 13:55
     */
    public function download($order_sn)
    {
        $xlsName = $order_sn;
        $xlsCell = array(
            array('goods_name', lang('商品名称')),
            array('sku_name', lang('规格名称')),
            array('order_sn', lang('订单号')),
            array('num', lang('数量')),
            array('name', lang('发货人')),
            array('phone', lang('电话')),
            array('province', lang('省')),
            array('city', lang('市')),
            array('area', lang('区')),
            array('address', lang('街道')),
            array('receiver_name', lang('收件人')),
            array('receiver_mobile', lang('收件人电话')),
            array('receiver_province', lang('省')),
            array('receiver_city', lang('市')),
            array('receiver_district', lang('区')),
            array('receiver_address', lang('街道')),
        );
        $xlsData = Db::name('order_goods_list')
            ->alias('ogl')
            ->field('ogl.goods_name,ogl.sku_name,ogl.order_sn,ogl.num,s.name,s.phone,s.province,s.city,s.area,s.address')
            ->where(['ogl.order_sn' => $order_sn])
            ->join('goods g', 'g.id=ogl.goods_id', 'left')
            ->order('g.sender_id')
            ->join('goods_express_sender s', 's.id=g.sender_id', 'left')
            ->select();
        foreach ($xlsData as $k => $v) {
            $receiver = Db::name('order_goods_info')->where(['order_sn' => $order_sn])->find();
            $excelData[$v['sender_id']]['list'][] = [
                'goods_name' => $v['goods_name'],
                'sku_name' => $v['sku_name'],
                'order_sn' => $v['order_sn'],
                'num' => $v['num'],
                'name' => $v['name'],
                'phone' => $v['phone'],
                'province' => $v['province'],
                'city' => $v['city'],
                'area' => $v['area'],
                'address' => $v['address'],
                'receiver_name' => $receiver['receiver_name'],
                'receiver_mobile' => $receiver['receiver_mobile'],
                'receiver_city' => $receiver['city'],
                'receiver_district' => $receiver['district'],
                'receiver_address' => $receiver['receiver_address'],
            ];
            $excelData[$v['sender_id']]['sender'] = $v['name'];
        }
        $excelData = array_values($excelData);
        $this->exportExcel($xlsName, $xlsCell, $excelData);
    }
    
    /**
     * 调起打印页面
     */
    public function getPrintHtml()
    {
        return $this->fetch();
    }

    /**
     * 打印发货单数据查询
     * @param $orderIds 字符戳 需要打印的订单ID
     */
    public function getShipmentOrderData()
    {
        // 接收参数
        $aids = $this->request->get('aids');

        // 参数校验
        if (empty($aids)) {
            return ['status' => '5000', 'msg' => lang('参数不能为空')];
        }

        // 查询订单数据
        $orderData = OrderModel::alias('o')
            ->where("o.aid IN($aids)")
            ->field("
                o.order_sn,FROM_UNIXTIME(o.create_time, '%Y-%m-%d %H:%i:%s') date_time,ogi.receiver_name,
                ogi.receiver_mobile,ogi.province,ogi.city,ogi.district,ogi.receiver_address,ogi.remark
            ")
            ->join('order_goods_info ogi', 'ogi.order_sn = o.order_sn', 'LEFT')
            ->select();
        // echo Db::getlastsql();die;
        if ($orderData) {
            foreach ($orderData as $key => $val) {
                $orderData[$key]['goods_list'] = OrderGoods::where(['order_sn' => $val['order_sn']])
                    ->field("goods_name,shop_price,num,sku_name,remark,sales_integral,is_pure_integral")
                    ->select()
                    ->each(function ($item) {
                        if ($item['is_pure_integral'] == 1) {
                            $item['shop_price'] = 0;
                        }
                    });
                $orderData[$key]['order_num'] = Db::name('order_goods_list')
                    ->where(['order_sn' => $val['order_sn']])
                    ->sum('num');
            }
        }

        return ['status' => '2000', 'msg' => lang('数据查询成功'), 'result' => $orderData];
    }
}
